/*
 * UserManagement.fx
 * Author Yu Min-ting
 * Created on 2009-7-14, 14:52:05
 */

package com.sjtu.fx.admin;

import javafx.scene.*;


import com.sjtu.fx.tools.*;
import com.sjtu.fx.tools.SwingTable.*;

import javafx.scene.layout.VBox;

import javafx.io.http.HttpRequest;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.System;
import org.json.JSONObject;

import org.json.JSONArray;

import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

import javafx.scene.layout.HBox;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;

import javafx.stage.Alert;
import javafx.scene.control.TextBox;

import javafx.ext.swing.SwingComboBox;
import javafx.ext.swing.SwingComboBoxItem;

public class User{
    public var name:String;
    public var id:String;
    public var score:String;
    public var date:String;
    public var dept:String;
}



public class UserManagement extends CustomNode {

    var panel:Panel;
    var users:User[] = bind users with inverse;
    public var selection: Integer;
    var addUser:AddNewUser;


    var userTable:SwingTable = SwingTable{
        columns: [
                    TableColumn {
                        text: "User Name"
                    },
                    TableColumn {
                        text: "Register Date"
                    },
                    TableColumn {
                        text: "Department"
                    },
                    TableColumn {
                        text: "Score"
                    }
                ]
        rows: bind for (u in users)
                    TableRow{
                        cells:[
                            TableCell {
                                text:bind u.name
                            },                          
                            TableCell {
                                text:bind u.date
                            },
                            TableCell {
                                text: bind u.dept
                            },
                             TableCell {
                                text:bind u.score
                            }
                        ]
                    }
        selection: bind selection with inverse

    }

    var scoreText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "User Check in Score: "
    };
     var scoreTextBox = SwingComboBox {
            items: [
                SwingComboBoxItem {
                    text: "0"
                    selected: true
                }SwingComboBoxItem {
                    text: "1"
                }SwingComboBoxItem {
                    text: "2"
                }SwingComboBoxItem {
                    text: "3"
                }SwingComboBoxItem {
                    text: "4"
                }SwingComboBoxItem {
                    text: "5"
                }SwingComboBoxItem {
                    text: "6"
                }SwingComboBoxItem {
                    text: "7"
                }SwingComboBoxItem {
                    text: "8"
                }SwingComboBoxItem {
                    text: "9"
                }SwingComboBoxItem {
                    text: "10"
                }
            ]
        }


    var userNameText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "User Name: "
    };
    var userNameTextBox = TextBox {
            columns: 12
            selectOnFocus: true
        }
    var userNameButton = Button {
            text: "Search"
            action: function() {
                if(userNameTextBox.text != ""){
                    // to do 
                }



            }
        }
     var todo = Button {
             text: "To-Dos"
             action: function() {
                 getUserCheckList();
             }
         }






    var scoreButton = Button {
            text: "Handle User's score"
            action: function() {
                if(selection>=0){
                    users[selection].score = scoreTextBox.text;
                    System.out.println("id:{users[selection].id}");
                    System.out.println("score:{users[selection].score}");
                    setUserScore(users[selection].id,scoreTextBox.text,"pass");
                    delete users[selection];
                }
            }
        }
    var refresh = Button {
            text: "Say No to This User"
            action: function() {
                alertConfirm.content = "Make Sure to delete this user:\n {users[selection].name}";
                alertConfirm.bm = this;
                
                alertConfirm.userId = users[selection].id;
                alertConfirm.userScore = scoreTextBox.text;
                alertConfirm.users = users;
                alertConfirm.selection = selection;
                alertConfirm.visible = true;
            //    if(Alert.confirm("Confirmaion","Make Sure to delete this user:\n {users[selection].name}")){
            //        setUserScore(users[selection].id,scoreTextBox.text,"reject");
            //        delete users[selection];
            //    }
            }
        }
    var addNewUser = Hyperlink {
            text: "Add New User>>"
            action: function() {
                addUser.visible = true;
            }
        }
        
    var details = Hyperlink {
            text: "See Details>>"
            action: function() {

            }
        }

   var alert = UIAlert {
         translateX:75
         translateY:200
         title:"ERROR"
         content:"Please input entire information"
         width:280
         height:120
         visible:false
         opacity:0.9

     }

     var alertConfirm = UserManageAlert {
         translateX:75
         translateY:200
         title:"Confirmaion"
         content:"Please input entire information"
         width:280
         height:120
         visible:false
         opacity:0.9
     }

    override protected function create() : Node {
        getUserCheckList();
        Group{
            content:[
                 panel = Panel {
                    width:    430
                    height:   550
                    title:    "User Management"
                    translateX: 0
                    translateY: 0
                },
                VBox{
                    spacing: 25
                    translateX:32 translateY: 60
                    content:[
                        HBox{
                            spacing:18
                            content:[userNameText,userNameTextBox,userNameButton,todo]
                        }

                        userTable,
                        HBox{
                            spacing:50
                            content:[scoreText,scoreTextBox]
                        },
                        HBox{
                            spacing:90
                            content:[refresh,scoreButton]
                        },
                        
                        details,addNewUser
                        

                        ]

                }
               addUser = AddNewUser{
                   visible : false;
               }
               alert,alertConfirm

            ]
        }
     }

function getUserCheckList(){
    HttpRequest {
        method:HttpRequest.POST
        location:"http://localhost:8764/JavaFXBack/UserCheckList"
       onOutput: function(os:OutputStream) {
             try {
                os.write("1".getBytes());
                os.flush();
            } finally {
                os.close();
            }
        }
        onInput: function(is: InputStream) {
        try {
                def data:DataInputStream = new DataInputStream(is);
                var json:JSONObject = JSONObject{};
                var result:String = data.readLine();
                json = new JSONObject(result);
                var idList:JSONArray = json.getJSONArray("idList");
                var scoreList:JSONArray = json.getJSONArray("scoreList");
                var nameList:JSONArray = json.getJSONArray("nameList");
                var dateList:JSONArray = json.getJSONArray("dateList");
                var departmentList:JSONArray = json.getJSONArray("departmentList");
                var i:Integer = 0;
                while(i<scoreList.length()){
                    var user = User{
                        id: idList.get(i).toString()
                        name: nameList.get(i).toString()
                        score:scoreList.get(i).toString()
                        date:dateList.get(i).toString()
                        dept:departmentList.get(i).toString()
                    }
                    users[i] = user;
                    i++;
                }

            } finally {
                is.close();
            }
        }
    }.start();
}

public function setUserScore(id:String,score:String,type:String){
    HttpRequest {
        method:HttpRequest.POST
        location:"http://localhost:8764/JavaFXBack/CheckUser"
       onOutput: function(os:OutputStream) {
            try {
                var json:JSONObject = JSONObject{};
                json.put("userId",id);
                json.put("score",score);
                json.put("checkResult", type);
                var temp:String = "obj={json.toString()}";
         //       System.out.println(temp);
                os.write(temp.getBytes());
                os.flush();
            } finally {
                os.close();
            }
        }
        onInput: function(is: InputStream) {
        try {
                def data:DataInputStream = new DataInputStream(is);
                var json:JSONObject = JSONObject{};
                var result:String = data.readLine();
                alert.title = "Result";
                alert.content = result;
                alert.visible = true;
               // Alert.inform(result);
            } finally {
                is.close();
            }
        }
    }.start();
}

}
